package com.sc.portal.service.impl;

import com.sc.common.pojo.CommonResult;
import com.sc.common.pojo.ScrollPicture;
import com.sc.common.utils.HttpClientUtil;
import com.sc.common.utils.JsonUtils;
import com.sc.pojo.TbContent;
import com.sc.portal.service.ContentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sc on 2017/10/19.
 */
@Service
public class ContentServiceimpl implements ContentService {

    @Value("${REST_BASE_URL}")
    private  String REST_BASE_URL;

    @Value("${REST_CONTENT_URL}")
    private  String REST_CONTENT_URL;

    @Value("${REST_CONTENT_AD1_CID}")
    private  String REST_CONTENT_AD1_CID;


    @Override
    public String getAd1List() {


        String json = HttpClientUtil.doGet(REST_BASE_URL+REST_CONTENT_URL+REST_CONTENT_AD1_CID);
        //json->pojo
        CommonResult commonResult = CommonResult.formatToList(json, TbContent.class);
        List<TbContent> contentList = (List<TbContent>) commonResult.getData();
        List<ScrollPicture> scrollPictures = new ArrayList<ScrollPicture>();
        for(TbContent item:contentList){
            ScrollPicture scrollPicture = new ScrollPicture();
            scrollPicture.setHref(item.getUrl());
            scrollPicture.setSrc(item.getPic());
            scrollPicture.setSrcB(item.getPic2());
            scrollPicture.setAlt(item.getTitle());
            scrollPictures.add(scrollPicture);
        }
        //pojo->json
        String result = JsonUtils.objectToJson(scrollPictures);
        return result;
    }
}
